package top.noteday.impl;

import java.util.List;
import java.util.Date;
import entity.Page;
import entity.result.EntityResult;
import enums.http.ResponseCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import utils.common.MD5Util;
import utils.common.StrUtil;
import entity.user.*;
import top.noteday.dao.user.UserDao;
import top.noteday.provider.user.UserService;

@Component
public class UserServiceImpl implements UserService {

	private Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);

	@Autowired(required = false)
	private UserDao userDao;

    /*
    @Override
    PageResult<UserDto> selectUser(UserVo userVo, Page page) {
        UserQuery userQuery = new UserQuery();
        PageResult<UserDto> result = new PageResult<>();

        // TODO 设置查询信息
        userQuery.setCreator(userId);
        userQuery.setPage(page);

        // 设置查询条数
        int count = userDao.selectUserCount(userQuery);
        result.setTotalCount(count);

        List<UserDto> list = new ArrayList<>();
        if (count > 0) {
            list = userDao.selectUser(userQuery);

            // 返回值信息缺少时，赋值默认值
            for(CommissionDto dto : list) {
                // TODO 设置返回值默认信息
                //if(StrUtil.isEmpty(dto.getBorrowerPhone())) { dto.setBorrowerPhone(""); }
                //if(StrUtil.isEmpty(dto.getBuildArea())) { dto.setBuildArea(new BigDecimal("0.00")); }
            }
        }

        result.setData(list);
        result.setCode(ResponseCodeEnum.SUCCESS.getCode());
        result.setMess(ResponseCodeEnum.SUCCESS.getMessage());
        result.setSuccess(true);
        return result;
    }
    */

    /**
     * 插入数据
     **/
    @Override
    public User selectById(Integer id) {
        UserQuery query = new UserQuery();
        query.setUserId(id);

        List<UserDto> list = userDao.selectByParam(query);

        return StrUtil.isEmpty(list) ? null : list.get(0);
    }

    @Override
    public EntityResult<UserDto> login(UserVo vo) {
        LOGGER.info("登录接口参数{} = ", vo);

        EntityResult<UserDto> result = new EntityResult<>();
        if (null == vo || StrUtil.isEmpty(vo.getLoginName()) || StrUtil.isEmpty(vo.getLoginPwd())) {
            return result.responseFail(ResponseCodeEnum.PARAM_ERROR, "账号或密码错误不能为空");
        }
        // 查询用户
        UserQuery query = new UserQuery();
        query.setLoginName(vo.getLoginName());
        query.setDeleted(0);
        List<UserDto> userList = userDao.selectByParam(query);
        if (StrUtil.isEmpty(userList)) {
            return result.responseFail(ResponseCodeEnum.PARAM_ERROR, "登录账号不存在");
        }
        // 是否可用
        // 密码是否正确
        // 获取加密后的登录密码
        MD5Util.loginPwd(vo.getLoginPwd());
        return result;
    }
}
